<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="120px">
      <el-form-item label="设备编号" prop="deviceNo">
        <el-input
          v-model="queryParams.deviceNo"
          placeholder="请输入设备编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设备状态" prop="situation">
        <el-select v-model="queryParams.situation" placeholder="请选择设备状态" clearable>
          <el-option
            v-for="dict in dict.type.device_situation"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="设备名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入设备名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设备类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择设备类型" clearable>
          <el-option
            v-for="dict in dict.type.device_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="部门id" prop="deptId">
        <el-input
          v-model="queryParams.deptId"
          placeholder="请输入部门id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="绑定用户id" prop="customerId">
        <el-input
          v-model="queryParams.customerId"
          placeholder="请输入绑定用户id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="绑定用户名称" prop="customerName">
        <el-input
          v-model="queryParams.customerName"
          placeholder="请输入绑定用户名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设备归属" prop="ownershipFlag">
        <el-select v-model="queryParams.ownershipFlag" placeholder="请选择设备归属" clearable>
          <el-option
            v-for="dict in dict.type.device_ownership_flag"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['device:device:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['device:device:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['device:device:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['device:device:export']"
        >导出
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <!--        <el-button
                  type="warning"
                  plain
                  icon="el-icon-upload"
                  size="mini"
                  @click="handleImport"
                  v-hasPermi="['device:device:import']"
                >导入
                </el-button>-->
        <el-dropdown
          split-button
          type="success"
          @click="importOpen = true;webkitdirectory = false"
          @command="handleCommand"
        >上传
          <el-dropdown-menu slot="dropdown">
            <el-dropdown-item command="uploadFile">上传文件</el-dropdown-item>
            <el-dropdown-item command="uploadFiles">上传文件夹</el-dropdown-item>

          </el-dropdown-menu>
        </el-dropdown>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="deviceList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="设备编号" align="center" prop="deviceNo"/>
      <el-table-column label="设备状态" align="center" prop="situation">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.device_situation" :value="scope.row.situation"/>
        </template>
      </el-table-column>
      <el-table-column label="设备名称" align="center" prop="name"/>
      <el-table-column label="设备类型" align="center" prop="type">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.device_type" :value="scope.row.type"/>
        </template>
      </el-table-column>
      <el-table-column label="部门id" align="center" prop="deptId"/>
      <el-table-column label="设备描述" align="center" prop="remark"/>
      <el-table-column label="绑定用户id" align="center" prop="customerId"/>
      <el-table-column label="绑定用户名称" align="center" prop="customerName"/>
      <el-table-column label="设备归属" align="center" prop="ownershipFlag">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.device_ownership_flag" :value="scope.row.ownershipFlag"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleAccount(scope.row)"
            v-hasPermi="['device:device:account']"
          >账号管理
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['device:device:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['device:device:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改设备管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="设备编号" prop="deviceNo">
          <el-input v-model="form.deviceNo" placeholder="请输入设备编号"/>
        </el-form-item>
        <el-form-item label="设备状态" prop="situation">
          <el-select v-model="form.situation" placeholder="请选择设备状态">
            <el-option
              v-for="dict in dict.type.device_situation"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="设备名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入设备名称"/>
        </el-form-item>
        <el-form-item label="设备类型" prop="type">
          <el-select v-model="form.type" placeholder="请选择设备类型">
            <el-option
              v-for="dict in dict.type.device_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <!--        <el-form-item label="部门id" prop="deptId">
                  <el-input v-model="form.deptId" placeholder="请输入部门id"/>
                </el-form-item>-->
        <el-form-item label="设备描述" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入设备描述"/>
        </el-form-item>
        <!--        <el-form-item label="绑定用户名称" prop="customerName">
                  <el-input v-model="form.customerName" placeholder="请输入绑定用户名称"/>
                </el-form-item>-->
        <el-form-item label="删除标志" prop="delFlag">
          <el-radio-group v-model="form.delFlag">
            <el-radio
              v-for="dict in dict.type.delete_flag"
              :key="dict.value"
              :label="dict.value"
            >{{ dict.label }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="设备归属" prop="ownershipFlag">
          <el-radio-group v-model="form.ownershipFlag">
            <el-radio
              v-for="dict in dict.type.device_ownership_flag"
              :key="dict.value"
              :label="dict.value"
            >{{ dict.label }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="设备二维码" prop="qrCode">
          <!--
                    <el-input v-model="form.qrCode" placeholder="请输入设备二维码"/>
          -->
          <el-image
            style="width: 100px; height: 100px"
            :src="form.qrCode"
            :preview-src-list="[form.qrCode]"
          >
          </el-image>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 添加或修改设备账号信息对话框 -->
    <el-dialog :title="title" :visible.sync="accountOpen" width="500px" append-to-body>
      <el-form ref="accountForm" :model="accountForm" :rules="rules" label-width="80px">
        <el-form-item label="鉴权信息" prop="authCode">
          <el-input v-model="accountForm.authCode" placeholder="请输入鉴权信息"/>
        </el-form-item>
        <el-form-item label="设备类型" prop="type">
          <el-select v-model="accountForm.type" placeholder="请选择设备类型">
            <el-option
              v-for="dict in dict.type.device_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="设备账号" prop="deviceAccount">
          <el-input v-model="accountForm.deviceAccount" placeholder="请输入设备账号"/>
        </el-form-item>
        <el-form-item label="设备状态" prop="status">
          <el-radio-group v-model="accountForm.status">
            <el-radio
              v-for="dict in dict.type.device_account_status"
              :key="dict.value"
              :label="dict.value"
            >{{ dict.label }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="设备编号" prop="deviceNo">
          <el-input v-model="accountForm.deviceNo" placeholder="请输入设备编号"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitAccountForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <importDevice :importOpen.sync="importOpen" :webkitdirectory="webkitdirectory" @importSuccess="getList()"
    ></importDevice>
  </div>
</template>

<script>
import { listDevice, getDevice, delDevice, addDevice, updateDevice } from '@/api/device/device'
import { getDeviceAccount, addDeviceAccount } from '@/api/device/deviceAccount'
import importDevice from '@/views/device/device/importDevice.vue'

export default {
  name: 'Device',
  components: { importDevice },
  dicts: ['delete_flag', 'device_type', 'device_ownership_flag', 'device_situation', 'device_account_status'],
  data() {
    return {
      importOpen: false,
      webkitdirectory: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 设备管理表格数据
      deviceList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      accountOpen: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        deviceNo: null,
        situation: null,
        name: null,
        type: null,
        deptId: null,
        customerId: null,
        customerName: null,
        ownershipFlag: null,
        qrCode: null
      },
      // 表单参数
      form: {},
      //设备账号
      accountForm: {},
      // 表单校验
      rules: {
        deviceNo: [
          { required: true, message: '设备编号不能为空', trigger: 'blur' }
        ],
        type: [
          { required: true, message: '设备类型不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    handleCommand(command) {

      if (command === 'uploadFiles') {
        this.webkitdirectory = true
        /*  this.$nextTick(() => {
           document.getElementsByClassName('el-upload__input')[0].webkitdirectory = true
         }) */
      }else{
        this.webkitdirectory = false
      }
      this.importOpen = true
    },
    /** 查询设备管理列表 */
    getList() {
      this.loading = true
      listDevice(this.queryParams).then(response => {
        this.deviceList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.accountOpen = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        deviceNo: null,
        situation: null,
        name: null,
        type: null,
        deptId: null,
        remark: null,
        customerId: null,
        customerName: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        ownershipFlag: null,
        qrCode: null
      }
      this.resetForm('form')
    },
    resetAccount() {
      this.accountForm = {
        deviceId: null,
        authCode: null,
        type: null,
        deviceAccount: null,
        status: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        deviceNo: null
      }
      this.resetForm('accountForm')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加设备管理'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getDevice(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = '修改设备管理'
      })
    },
    /** 新增按钮操作 */
    handleAccount(row) {
      this.resetAccount()
      const id = row.id || this.ids
      getDeviceAccount(id).then(response => {
        this.accountForm = response.data ? response.data : {}
        this.accountForm.deviceId = id
        this.accountForm.deviceNo = row.deviceNo
        this.accountOpen = true
        this.title = '设备账号管理'
      })
    },
    /** 提交按钮 */
    submitAccountForm() {
      this.$refs['accountForm'].validate(valid => {
        if (valid) {
          addDeviceAccount(this.accountForm).then(response => {
            this.$modal.msgSuccess('操作成功')
            this.accountOpen = false
          })
        }
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateDevice(this.form).then(response => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addDevice(this.form).then(response => {
              this.$modal.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除设备管理编号为"' + ids + '"的数据项？').then(function() {
        return delDevice(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/device/export', {
        ...this.queryParams
      }, `device_${new Date().getTime()}.xlsx`)
    },
    /** 导入按钮操作 */
    handleImport() {
      this.importOpen = true
    }
  }
}
</script>
